from fastapi import APIRouter
from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.core.mlamada import bools_string
from re_common.baselibrary.utils.core.requests_core import MsgCode

from apps.allsubdb.cnkijournal.models import headers
from apps.allsubdb.cnkithesis.models import SchoolListModel
from apps.core.return_info import InputInfoModel, ReturnInfo
import types

from apps.crawler_platform.util.requestapihelper import RequestApiHelper

router = APIRouter()

@router.post("/step1/school_list")
async def down_schoollist(input: InputInfoModel[SchoolListModel]):
    """
    下载博硕学校列表
    :param proxy:
    :return:
    """
    return_info = ReturnInfo()
    marks = input.data.marks
    proxy = input.proxy
    this_header = headers.copy()
    knsUrl = r'https://navi.cnki.net/KNavi/Common/Search/PPaper'
    rrq = RequestApiHelper.get_rrq()
    kwargs = {
        "rrq": rrq,
        "header": this_header,
        "url": knsUrl,
        "timeout": 30,
        "marks": marks,
        "proxy": proxy,
        "moths": MRequest.GET,
        "middler_list": [rrq.status_code_middlerwares, rrq.marks_middlerwares, rrq.end_middlerwares]
    }
    bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
    # rrq = AioHttpRequest()
    # rrq.set_header(this_header).set_url(knsUrl) \
    #     .set_timeout(30).set_marks(marks) \
    #     .set_proxy(proxy) \
    #     .set_middler_list(
    #     [rrq.status_code_middlerwares, rrq.have_end_middlerwares,rrq.marks_middlerwares])
    # bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()


@router.post("/step2/article_list")
async def down_articlelist(baseid: str, page: str,name: str, proxy: str = None):
    """
    下载学校下博硕文章列表
    :param pykm:
    :param proxy:
    :return:
    """
    pass

@router.post("/step3/article")
async def down_article(filename:str,dbcode:str,years:str,proxy: str = None):
    """
    下载博硕文章详情
    :param pykm:
    :param proxy:
    :return:
    """
    pass
